/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package xmldatabase;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author betty
 */
public class dom4jXmlDemo {

    private Document document;

    //获取DOM树
    public void getDom() {
        SAXReader reader = new SAXReader();

        try {

            document = reader.read(new File("C:\\Users\\betty\\Desktop\\info.xml"));

        } catch (DocumentException e) {
        }

    }

    //增加手机信息
    public void addPhoneInfo() {

        //获取根节点元素
        Element element = document.getRootElement();

        //添加brand节点
        Element brand = element.addElement("brand");

        //为brand添加属性并且赋值
        brand.addAttribute("name", "苹果");

        //添加type节点
        Element type = brand.addElement("type");

        Element type1 = brand.addElement("type1");

        //为type节点添加name属性并且赋值
        type.addAttribute("name", "iphone7");

        type1.addAttribute("name", "iphone6");

        saveXml("phoneinfo.xml");

    }

    //删除手机信息
    public void deletePhoneInfo() {

        //获取根节点
        Element element = document.getRootElement();

        Iterator brand = element.elementIterator();

        //遍历获取每个节点信息
        while (brand.hasNext()) {

            Element eleBrand = (Element) brand.next();

            //删除name为苹果的brand
            if (eleBrand.attributeValue("name").equals("苹果")) {

                eleBrand.getParent().remove(eleBrand);

            }

        }

        //保存信息
        saveXml("info.xml");

    }

    //修改手机信息
    public void updatePhoneInfo() {

        //获取手机信息
        Element element = document.getRootElement();

        Iterator brand = element.elementIterator();

        int id = 0;

        while (brand.hasNext()) {

            Element eleBrand = (Element) brand.next();

            id++;

            eleBrand.addAttribute("id", id + "");

        }

        saveXml("info.xml");

    }

    //显示手机信息
    public void showPhoneInfo() {

        //获取根节点
        Element element = document.getRootElement();

        Iterator brand = element.elementIterator();

        while (brand.hasNext()) {

            Element eleBrand = (Element) brand.next();
            //根据name属性获取对应的值的名字
            System.out.println(eleBrand.attributeValue("name"));
            //获取type节点
            Iterator type = eleBrand.elementIterator();

            //遍历所有type节点
            while (type.hasNext()) {

                Element eleType = (Element) type.next();

                System.out.println("\t" + eleType.attributeValue("name"));

            }

        }

    }

    //保存信息到xml
    public void saveXml(String path) {
        //格式化
        OutputFormat format = OutputFormat.createPrettyPrint();
        //指定字符编码格式
        format.setEncoding("UTF-8");
        XMLWriter writer = null;
        //将文件按照指定格式输出到指定路径中
        try {
            writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(path)), format);
            writer.write(document);

        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {

            try {

                //关闭服务
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();

            }

        }

    }

}
